//
//  SCHelper.m
//  Shuaiche
//
//  Created by zzs on 2018/5/3.
//  Copyright © 2018年 zzs. All rights reserved.
//

#import "SCHelper.h"
#import "UIView+InputHandler.h"

@implementation SCHelper


+ (BOOL)inputView:(UIView<UITextInput> *)inputView shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string maxLength:(NSInteger)maxLength completion:(void(^)(NSString *result,UIView<UITextInput> *inputView))completion{
    if(![inputView isKindOfClass:[UITextField class]]&&![inputView isKindOfClass:[UITextView class]]){
        return NO;
    }
    //删除键
    if(![string length])return YES;
    if(inputView.scEmojiUnEnabled){
        if([SCHelper stringContainsEmoji:string]){
            return NO;
        }
    }
    if(inputView.scIntNumberOnly){
        if(![SCHelper isValidatedNumber:string]){
            return NO;
        }
    }
    if(inputView.scFloatNumberOnly){
        if(![SCHelper isValidatedFloatNumber:string]){
            return NO;
        }
    }
    NSString *tempStr = [((NSString *)[inputView valueForKey:@"text"]) stringByReplacingCharactersInRange:range withString:string];
    //剩余能输入的长度
    NSInteger len = maxLength - tempStr.length;
    if(len>=0){
        return YES;
    }else{
        //比如点击中文提示的文字
        len = string.length + len;
        //防止不合法的len出现
        NSRange fixedRange = {0,MAX(len,0)};
        if(fixedRange.length>0){
            NSString *fixedString = [self handleEmojiString:string maxLength:fixedRange.length];
            //NSString *fixedString = [string substringWithRange:fixedRange];
            //rang是指从当前光标处进行替换处理(注意如果执行此句后面返回的是YES会触发didchange事件),或者修改先textField.delegate = nil, setText, 再 textField.delegate = 原先的
            NSString *replacedStr = [((NSString *)[inputView valueForKey:@"text"]) stringByReplacingCharactersInRange:range withString:fixedString];
            [inputView setValue:replacedStr forKey:@"text"];
            if(completion){
                completion(replacedStr,inputView);
            }
            return NO;
        }
        //获取高亮部分
        UITextRange *markedRange = [inputView markedTextRange];
        UITextPosition *position = [inputView positionFromPosition:markedRange.start offset:0];
        if(markedRange&&position){//如果有高亮
            NSInteger startOffset = [inputView offsetFromPosition:inputView.beginningOfDocument toPosition:markedRange.start];
            NSInteger endOffset = [inputView offsetFromPosition:inputView.beginningOfDocument toPosition:markedRange.end];
            NSRange offsetRange = NSMakeRange(startOffset, endOffset - startOffset);
            if(offsetRange.location < maxLength){
                return YES;
            }else{
                return NO;
            }
        }
        return NO;
    }
}


+ (BOOL)customInputView:(UIView *)inputView shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;{
   return [self inputView:inputView shouldChangeCharactersInRange:range replacementString:string maxLength:NSIntegerMax];
}

+ (BOOL)inputView:(UIView <UITextInput>*)inputView shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string maxLength:(NSInteger)maxLength{
    return [self inputView:inputView shouldChangeCharactersInRange:range replacementString:string maxLength:maxLength completion:nil];
//    if(![inputView isKindOfClass:[UITextField class]]&&![inputView isKindOfClass:[UITextView class]]){
//        return NO;
//    }
//    //删除键
//    if(![string length])return YES;
//    if(inputView.scEmojiUnEnabled){
//        if([SCHelper stringContainsEmoji:string]){
//            return NO;
//        }
//    }
//    if(inputView.scIntNumberOnly){
//        if(![SCHelper isValidatedNumber:string]){
//            return NO;
//        }
//    }
//    if(inputView.scFloatNumberOnly){
//        if(![SCHelper isValidatedFloatNumber:string]){
//            return NO;
//        }
//    }
//    NSString *tempStr = [((NSString *)[inputView valueForKey:@"text"]) stringByReplacingCharactersInRange:range withString:string];
//    //剩余能输入的长度
//    NSInteger len = maxLength - tempStr.length;
//    if(len>=0){
//        return YES;
//    }else{
//        //比如点击中文提示的文字
//        len = string.length + len;
//        //防止不合法的len出现
//        NSRange fixedRange = {0,MAX(len,0)};
//        if(fixedRange.length>0){
//            NSString *fixedString = [string substringWithRange:fixedRange];
//            //rang是指从当前光标处进行替换处理(注意如果执行此句后面返回的是YES会触发didchange事件),或者修改先textField.delegate = nil, setText, 再 textField.delegate = 原先的
//            //此处不考虑emoji表情的长度等
//            NSString *testStr = [((NSString *)[inputView valueForKey:@"text"]) stringByReplacingCharactersInRange:range withString:fixedString];
//            [inputView setValue:testStr forKey:@"text"];
//            return NO;
//        }
//        //获取高亮部分
//        UITextRange *markedRange = [inputView markedTextRange];
//        UITextPosition *position = [inputView positionFromPosition:markedRange.start offset:0];
//        if(markedRange&&position){//如果有高亮
//            NSInteger startOffset = [inputView offsetFromPosition:inputView.beginningOfDocument toPosition:markedRange.start];
//            NSInteger endOffset = [inputView offsetFromPosition:inputView.beginningOfDocument toPosition:markedRange.end];
//            NSRange offsetRange = NSMakeRange(startOffset, endOffset - startOffset);
//            if(offsetRange.location < maxLength){
//                return YES;
//            }else{
//                return NO;
//            }
//        }
//        return NO;
//    }
}

+ (BOOL)numberInputView:(UIView *)inputView shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if(![inputView isKindOfClass:[UITextField class]]&&![inputView isKindOfClass:[UITextView class]]){
        return NO;
    }
    //删除键
    if(![string length])return YES;
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    if(inputView.scEmojiUnEnabled){
        if([SCHelper stringContainsEmoji:string]){
            return NO;
        }
    }
     NSString *contentStr = [inputView valueForKey:@"text"];
    if(inputView.scIntNumberOnly){
        if(![SCHelper isValidatedNumber:string]||(contentStr.length&&![numberFormatter numberFromString:contentStr])){
            return NO;
        }
    }
    if(inputView.scFloatNumberOnly){
        if(![SCHelper isValidatedFloatNumber:string]||(contentStr.length&&![numberFormatter numberFromString:contentStr])){
            return NO;
        }
    }
    if(range.location==0){
        if([string characterAtIndex:0]=='0'){
            if(((NSString *)[inputView valueForKey:@"text"]).length>0){
                //只有当第一个是"."的时候,允许在前面第一位添加0
                if([((NSString *)[inputView valueForKey:@"text"]) characterAtIndex:0]=='.'){
                    return YES;
                }
                return NO;
            }
        }
    }
    //第一位为0第二位只能是小数点
    if(((NSString *)[inputView valueForKey:@"text"]).length&&[((NSString *)[inputView valueForKey:@"text"]) characterAtIndex:0]=='0'){
        if(range.location==1){
            if ([string characterAtIndex:0]!='.') {
                return NO;
            }
        }
    }
    if([((NSString *)[inputView valueForKey:@"text"]) rangeOfString:@"."].location==NSNotFound){
        if([string characterAtIndex:0]=='.'){
            if(range.location==0){//第一位不能是小数点
                return NO;
            }
        }
        NSString *testStr = [((NSString *)[inputView valueForKey:@"text"]) stringByReplacingCharactersInRange:range withString:string];
        if([testStr longLongValue]*10000<= INT_MAX&&[testStr longLongValue]*10000>=0){
            return YES;
        }else{
            return NO;
        }
    }else{
        if([string characterAtIndex:0]=='.'){
            return NO;
        }else{
            NSRange pointRange = [((NSString *)[inputView valueForKey:@"text"]) rangeOfString:@"."];
            if(range.location<=2+pointRange.location){
                if((range.location>pointRange.location) && ([((NSString *)[inputView valueForKey:@"text"]) length]>2+pointRange.location)){
                    return NO;
                }
            }else{
                return NO;
            }
        }
        NSString *testStr = [((NSString *)[inputView valueForKey:@"text"]) stringByReplacingCharactersInRange:range withString:string];
        if([testStr longLongValue]*10000<= INT_MAX&&[testStr longLongValue]*10000>=0){
            return YES;
        }else{
            return NO;
        }
    }
}
//利率 小于等于100 两位小数 
+ (BOOL)numberRateInputView:(UIView *)inputView shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if(![inputView isKindOfClass:[UITextField class]]&&![inputView isKindOfClass:[UITextView class]]){
        return NO;
    }
    //删除键
    if(![string length])return YES;
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    if(inputView.scEmojiUnEnabled){
        if([SCHelper stringContainsEmoji:string]){
            return NO;
        }
    }
     NSString *contentStr = [inputView valueForKey:@"text"];
    if(inputView.scIntNumberOnly){
        if(![SCHelper isValidatedNumber:string]||(contentStr.length&&![numberFormatter numberFromString:contentStr])){
            return NO;
        }
    }
    if(inputView.scFloatNumberOnly){
        if(![SCHelper isValidatedFloatNumber:string]||(contentStr.length&&![numberFormatter numberFromString:contentStr])){
            return NO;
        }
    }
    if(range.location==0){
        if([string characterAtIndex:0]=='0'){
            if(((NSString *)[inputView valueForKey:@"text"]).length>0){
                //只有当第一个是"."的时候,允许在前面第一位添加0
                if([((NSString *)[inputView valueForKey:@"text"]) characterAtIndex:0]=='.'){
                    return YES;
                }
                return NO;
            }
        }
    }
    //第一位为0第二位只能是小数点
    if(((NSString *)[inputView valueForKey:@"text"]).length&&[((NSString *)[inputView valueForKey:@"text"]) characterAtIndex:0]=='0'){
        if(range.location==1){
            if ([string characterAtIndex:0]!='.') {
                return NO;
            }
        }
    }
    if([((NSString *)[inputView valueForKey:@"text"]) rangeOfString:@"."].location==NSNotFound){
        if([string characterAtIndex:0]=='.'){
            if(range.location==0){//第一位不能是小数点
                return NO;
            }
        }
        
        NSString *testStr = [((NSString *)[inputView valueForKey:@"text"]) stringByReplacingCharactersInRange:range withString:string];
        if([testStr longLongValue]<= 100&&[testStr longLongValue]>=0){
            if ([testStr isEqualToString:@"100."]) {//100之后不可以再输入小数
                return NO;
            }
            return YES;
        }else{
            return NO;
        }
    }else{
        if([string characterAtIndex:0]=='.'){
            return NO;
        }else{
            NSRange pointRange = [((NSString *)[inputView valueForKey:@"text"]) rangeOfString:@"."];
            if(range.location<=2+pointRange.location){
                if((range.location>pointRange.location) && ([((NSString *)[inputView valueForKey:@"text"]) length]>2+pointRange.location)){
                    return NO;
                }
            }else{
                return NO;
            }
        }
        NSString *testStr = [((NSString *)[inputView valueForKey:@"text"]) stringByReplacingCharactersInRange:range withString:string];
        if([testStr longLongValue]<= 100&&[testStr longLongValue]>=0){
            return YES;
        }else{
            return NO;
        }
    }
}

+ (BOOL)inputView:(UIView *)inputView shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if(![inputView isKindOfClass:[UITextField class]]&&![inputView isKindOfClass:[UITextView class]]){
        return NO;
    }
    //删除键
    if(![string length])return YES;
    if(inputView.scEmojiUnEnabled){
        if([SCHelper stringContainsEmoji:string]){
            return NO;
        }
    }
    if(inputView.scIntNumberOnly){
        if(![SCHelper isValidatedNumber:string]){
            return NO;
        }
    }
    if(inputView.scFloatNumberOnly){
        if(![SCHelper isValidatedFloatNumber:string]){
            return NO;
        }
    }
    if(range.location==0){
        if([string characterAtIndex:0]=='0'){
            if(((NSString *)[inputView valueForKey:@"text"]).length>0){
                //只有当第一个是"."的时候,允许在前面第一位添加0
                if([((NSString *)[inputView valueForKey:@"text"]) characterAtIndex:0]=='.'){
                    return YES;
                }
                return NO;
            }
        }
    }
    //第一位为0第二位只能是小数点
    if(((NSString *)[inputView valueForKey:@"text"]).length&&[((NSString *)[inputView valueForKey:@"text"]) characterAtIndex:0]=='0'){
        if(range.location==1){
            if ([string characterAtIndex:0]!='.') {
                return NO;
            }
        }
    }
    if([((NSString *)[inputView valueForKey:@"text"]) rangeOfString:@"."].location==NSNotFound){
        if([string characterAtIndex:0]=='.'){
            if(range.location==0){//第一位不能是小数点
                return NO;
            }
        }
        NSString *testStr = [((NSString *)[inputView valueForKey:@"text"]) stringByReplacingCharactersInRange:range withString:string];
        if([testStr longLongValue]*10000<= INT_MAX&&[testStr longLongValue]*10000>=0){
            return YES;
        }else{
            return NO;
        }
    }else{
        if([string characterAtIndex:0]=='.'){
            return NO;
        }else{
            NSRange pointRange = [((NSString *)[inputView valueForKey:@"text"]) rangeOfString:@"."];
            if(range.location<=2+pointRange.location){
                if((range.location>pointRange.location) && ([((NSString *)[inputView valueForKey:@"text"]) length]>2+pointRange.location)){
                    return NO;
                }
            }else{
                return NO;
            }
        }
        NSString *testStr = [((NSString *)[inputView valueForKey:@"text"]) stringByReplacingCharactersInRange:range withString:string];
        if([testStr longLongValue]*10000<= INT_MAX&&[testStr longLongValue]*10000>=0){
            return YES;
        }else{
            return NO;
        }
    }
}

+ (BOOL)isValidatedNumber:(NSString *)number{
    NSString *phoneRegex = @"^\\d$";
    NSPredicate *phonePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
    return [phonePredicate evaluateWithObject:number];
}

+ (BOOL)isValidatedFloatNumber:(NSString *)floatNumber;{
    NSString *phoneRegex = @"^[\\d|.]$";
    NSPredicate *phonePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
    return [phonePredicate evaluateWithObject:floatNumber];
}


+ (BOOL)isValidateNumberInputView:(UIView<UITextInput> *)inputView shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string maxLength:(NSInteger)maxLength;{
    //删除键
    if(![string length])return YES;
    if(![SCHelper isValidatedNumber:string]){
        return NO;
    }
    //第一位为0,则不允许继续输入
    if(((NSString *)[inputView valueForKey:@"text"]).length&&[((NSString *)[inputView valueForKey:@"text"]) characterAtIndex:0]=='0'){
        if(range.location==1){
            return NO;
        }
    }
    NSString *tempStr = [((NSString *)[inputView valueForKey:@"text"]) stringByReplacingCharactersInRange:range withString:string];
    //剩余能输入的长度
    NSInteger len = maxLength - tempStr.length;
    if(len>=0){
        return YES;
    }else{
        //比如点击中文提示的文字
        len = string.length + len;
        //防止不合法的len出现
        NSRange fixedRange = {0,MAX(len,0)};
        if(fixedRange.length>0){
            NSString *fixedString = [string substringWithRange:fixedRange];
            //rang是指从当前光标处进行替换处理(注意如果执行此句后面返回的是YES会触发didchange事件),或者修改先textField.delegate = nil, setText, 再 textField.delegate = 原先的
            //此处不考虑emoji表情的长度等
//            [textField setText:[textField.text stringByReplacingCharactersInRange:range withString:fixedString]];
            NSString *replacedStr = [((NSString *)[inputView valueForKey:@"text"]) stringByReplacingCharactersInRange:range withString:fixedString];
            [inputView setValue:replacedStr forKey:@"text"];
            return NO;
        }
        //获取高亮部分
        UITextRange *markedRange = [inputView markedTextRange];
        UITextPosition *position = [inputView positionFromPosition:markedRange.start offset:0];
        if(markedRange&&position){//如果有高亮
            NSInteger startOffset = [inputView offsetFromPosition:inputView.beginningOfDocument toPosition:markedRange.start];
            NSInteger endOffset = [inputView offsetFromPosition:inputView.beginningOfDocument toPosition:markedRange.end];
            NSRange offsetRange = NSMakeRange(startOffset, endOffset - startOffset);
            if(offsetRange.location < maxLength){
                return YES;
            }else{
                return NO;
            }
        }
        return NO;
    }
}
//在should begin的时候不用考虑selectedTextRange
+ (BOOL)isValidatedStockWarningTextField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string maxLength:(NSInteger)maxLength{
    //删除键
    if(![string length])return YES;
    if(textField.scIntNumberOnly){
        if(![SCHelper isValidatedNumber:string]){
            return NO;
        }
    }
    //第一位为0,则不允许继续输入
    if(textField.text.length&&[textField.text characterAtIndex:0]=='0'){
        if(range.location==1){
            return NO;
        }
    }
     NSString *tempStr = [textField.text stringByReplacingCharactersInRange:range withString:string];
    //剩余能输入的长度
     NSInteger len = maxLength - tempStr.length;
     if(len>=0){
         return YES;
     }else{
         //比如点击中文提示的文字
          len = string.length + len;
         //防止不合法的len出现
         NSRange fixedRange = {0,MAX(len,0)};
         if(fixedRange.length>0){
             NSString *fixedString = [string substringWithRange:fixedRange];
            //rang是指从当前光标处进行替换处理(注意如果执行此句后面返回的是YES会触发didchange事件),或者修改先textField.delegate = nil, setText, 再 textField.delegate = 原先的
             //此处不考虑emoji表情的长度等
             [textField setText:[textField.text stringByReplacingCharactersInRange:range withString:fixedString]];
             return NO;
         }
         //获取高亮部分
         UITextRange *markedRange = [textField markedTextRange];
         UITextPosition *position = [textField positionFromPosition:markedRange.start offset:0];
         if(markedRange&&position){//如果有高亮
             NSInteger startOffset = [textField offsetFromPosition:textField.beginningOfDocument toPosition:markedRange.start];
             NSInteger endOffset = [textField offsetFromPosition:textField.beginningOfDocument toPosition:markedRange.end];
             NSRange offsetRange = NSMakeRange(startOffset, endOffset - startOffset);
             if(offsetRange.location < maxLength){
                 return YES;
             }else{
                 return NO;
             }
         }
         return NO;
     }
}

+ (void)stockWarningTextField:(UITextField *)textField maxLength:(NSInteger)maxLength
{
    UITextRange *markedTextRange = [textField markedTextRange];
    //获取高亮部分
    UITextPosition *position = [textField positionFromPosition:markedTextRange.start offset:0];
    
    //如果是高亮部分引起的变化则继续执行
    if (markedTextRange && position) {
        return;
    }
    NSString  *contentString = textField.text;
    NSInteger totalLength = contentString.length;
    
    if (totalLength > maxLength)
    {
        //截取到最大位置的字符(由于超出截部分在should时被处理了所在这里这了提高效率不再判断)
        // NSString *s = [nsTextContent substringToIndex:MAX_LIMIT_NUMS];
        [textField setText:[contentString substringToIndex:maxLength]];
    }
}



+ (BOOL)isMobileNumber:(NSString *)mobileNum {
    
    NSString *MOBILE = @"^\\d{11}$";
    
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    
    return [regextestmobile evaluateWithObject:mobileNum];
    
}

+ (BOOL)isFixedTelephone:(NSString *)phoneNum {
    
    /**
     
     * 大陆地区固话及小灵通
     
     * 区号：010,020,021,022,023,024,025,027,028,029
     
     * 号码：七位或八位
     
     */
    
    NSString * FT = @"^(0[0-9]{2})\\d{8}$|^(0[0-9]{3}(\\d{7,8}))$";
    
    NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", FT];
    
    return [regextestcm evaluateWithObject:phoneNum];
    
}


/**
 注意:string.length 是根据unicode编码长度计算的，有时候一个emoji表情占用2个unicode编码（中文基本都是一个），所以导致判断异常

 @param emojiString 原始字符串长度
 @param maxLength 最大字节数量
 @return 处理后的字符串
 */
+ (NSString *)handleEmojiString:(NSString *)emojiString maxLength:(NSInteger)maxLength{
    NSString *resultString = nil;
    BOOL canBeConvertToASCII = [emojiString canBeConvertedToEncoding:NSASCIIStringEncoding];
    if(canBeConvertToASCII){
        resultString = [emojiString substringToIndex:maxLength];
    }else{
        __block NSInteger idx = 0;
        __block NSString  *trimmedString = @"";//截取出的字串
        //使用字符串遍历，这个方法能准确知道每个emoji是占一个unicode还是两个
        [emojiString enumerateSubstringsInRange:NSMakeRange(0, [emojiString length])
                                         options:NSStringEnumerationByComposedCharacterSequences
                                      usingBlock: ^(NSString* substring, NSRange substringRange, NSRange enclosingRange, BOOL* stop) {
                                          
                                          if (idx >= maxLength) {
                                              *stop = YES; //取出所需要就break，提高效率
                                              return ;
                                          }
                                          trimmedString = [trimmedString stringByAppendingString:substring];
                                          idx++;
                                      }];
        
        resultString = trimmedString;
    }
    return resultString;
}

+ (BOOL)stringContainsEmojiBeyondOther:(NSString *)string {
    static NSString *other = @"[➋➌➍➎➏➐➑➒]";
    NSRegularExpression *exp = [NSRegularExpression regularExpressionWithPattern:other options:kNilOptions error:nil];
    NSUInteger num = [exp numberOfMatchesInString:string options:kNilOptions range:NSMakeRange(0, string.length)];
    return num;
}

+ (BOOL)stringContainsEmoji:(NSString *)string {
    
    if ([self stringContainsEmojiBeyondOther:string]) {
        return NO;
    }
    
    __block BOOL returnValue = NO;
    
    [string enumerateSubstringsInRange:NSMakeRange(0, [string length])
                               options:NSStringEnumerationByComposedCharacterSequences
                            usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
                                const unichar hs = [substring characterAtIndex:0];
                                if (0xd800 <= hs && hs <= 0xdbff) {
                                    if (substring.length > 1) {
                                        const unichar ls = [substring characterAtIndex:1];
                                        const int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
                                        if (0x1d000 <= uc && uc <= 0x1f77f) {
                                            returnValue = YES;
                                        }
                                    }
                                } else if (substring.length > 1) {
                                    const unichar ls = [substring characterAtIndex:1];
                                    if (ls == 0x20e3) {
                                        returnValue = YES;
                                    }
                                } else {
                                    if (0x2100 <= hs && hs <= 0x27ff) {
                                        returnValue = YES;
                                    } else if (0x2B05 <= hs && hs <= 0x2b07) {
                                        returnValue = YES;
                                    } else if (0x2934 <= hs && hs <= 0x2935) {
                                        returnValue = YES;
                                    } else if (0x3297 <= hs && hs <= 0x3299) {
                                        returnValue = YES;
                                    } else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50) {
                                        returnValue = YES;
                                    }
                                }
                            }];
    
    return returnValue;
}

+ (NSString *)filterEmoji:(NSString *)string {
    NSUInteger len = [string lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
    const char *utf8 = [string UTF8String];
    char *newUTF8 = malloc( sizeof(char) * len );
    int j = 0;
    
    //0xF0(4) 0xE2(3) 0xE3(3) 0xC2(2) 0x30---0x39(4)
    for ( int i = 0; i < len; i++ ) {
         unsigned int c = utf8;
        BOOL isControlChar = NO;
        if ( c == 4294967280 ||
            c == 4294967089 ||
            c == 4294967090 ||
            c == 4294967091 ||
            c == 4294967092 ||
            c == 4294967093 ||
            c == 4294967094 ||
            c == 4294967095 ||
            c == 4294967096 ||
            c == 4294967097 ||
            c == 4294967088 ) {
            i = i + 3;
            isControlChar = YES;
        }
        if ( c == 4294967266 || c == 4294967267 ) {
            i = i + 2;
            isControlChar = YES;
        }
        if ( c == 4294967234 ) {
            i = i + 1;
            isControlChar = YES;
        }
        if ( !isControlChar ) {
            newUTF8[j] = utf8;
            j++;
        }
    }
    newUTF8[j] = '\0';
    NSString *encrypted = [NSString stringWithCString:(const char*)newUTF8
                                             encoding:NSUTF8StringEncoding];
    free( newUTF8 );
    return encrypted;
}

@end
